//
//  ApplyInvoiceViewModel.m
//  GLGW_Shoping
//
//  Created by whx on 2018/12/10.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "ApplyInvoiceViewModel.h"
#import "LoginModel.h"

@implementation ApplyInvoiceViewModel

- (instancetype)initWithSuperView:(UIView *)superView
{
    if (self = [super initWithSuperView:superView]) {
        superView.backgroundColor = [UIColor colorWithHexString:@"#FAFAFA"];
        
        GLGWView *baView = [[GLGWView alloc] initWithInSuperView:superView];
        baView.backgroundColor = [UIColor whiteColor];
        CGFloat vH = KScreenHeight - kHeightProportion_In_iPhoneTriploid(64);
        if (iPhoneX) {
            vH = KScreenHeight - kHeightProportion_In_iPhoneTriploid(88) - kHeightProportion_In_iPhoneTriploid(34);
        }
        [baView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.equalTo(superView);
            make.height.equalTo(@(vH));
        }];
        
        GLGWView *topV = [[GLGWView alloc] initWithInSuperView:superView];
        topV.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        [topV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(superView);
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(10)));
        }];

        GLGWLabel *lab = [[GLGWLabel alloc] initWithFont:SYSTEMFONT(16) theColor:kColor_Rule_Black inSuperView:baView];
        lab.text = @"您可以上传专票模版照片";
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(baView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(15));
            make.top.equalTo(topV.mas_bottom).offset(kWidthProportion_In_iPhoneTriploid(20));
        }];
        
        GLGWButton *btn = [[GLGWButton alloc] initInSuperView:baView];
        [btn setImage:[UIImage imageNamed:@"icon_xiangji"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_fapiao"] forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(topV.mas_bottom).offset(kWidthProportion_In_iPhoneTriploid(82));
            make.left.equalTo(baView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(80));
            make.right.equalTo(baView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(81));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(135)));
        }];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        GLGWButton *bottomBtn = [[GLGWButton alloc] initWithTitle:@"申请开票" titleLableFont:SYSTEMFONT(16) titleLableColor:[UIColor colorWithHexString:@"#FFFFFF"] inSuperView:baView];
        [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_bottom).offset(kWidthProportion_In_iPhoneTriploid(41));
            make.left.equalTo(baView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(15));
            make.right.equalTo(baView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(15));
            make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(44)));
        }];
        [bottomBtn setBackgroundImage:[UIImage imageWithStartColor:[UIColor colorWithHexString:@"#A89A60"] endColor:[UIColor colorWithHexString:@"#A89A60" alpha:0.8]] forState:UIControlStateNormal];
        bottomBtn.layer.masksToBounds = YES;
        bottomBtn.layer.cornerRadius = kHeightProportion_In_iPhoneTriploid(22);
        [bottomBtn addTarget:self action:@selector(bottomAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}

- (void)btnAction:(GLGWButton *)sender
{
   // 照照片
}

- (void)bottomAction:(GLGWButton *)sender
{
    if (self.BlockTakeApply) {
        self.BlockTakeApply(sender);
    }
}


#pragma mark - 请求申请发票接口
- (void)applyInvoice
{
    if (self.userInforData.length == 0) return;
    LoginModel *modles = [NSKeyedUnarchiver unarchiveObjectWithData:self.userInforData];
    
    NSDictionary *param = @{
                            @"upType" : @(2),                    // 发票类型  1.填写增票物质信息  2.上传增票物质信息
                            @"userId" : koString(modles.data.userId),
                            @"objectUrl" : koString(@""),        // 上传的对象地址 word地址或者图片地址 阿里云OSS存
                            @"orderId" : koString(@""),          // 订单id
                            };
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kApplyInvoice] lodingView:nil lodingTextString:@"" isLoding:YES parameters:param success:^(id  _Nullable response) {

        BaseModel *model = [BaseModel yy_modelWithJSON:response];
        if (model.code == 1) { // 未成功
            DLog(@"%@",model.message);
        } else {
            // 成功：这里看需要做什么操作
        }
    } failure:^(NSError * _Nullable error) {

    }];

}
@end
